﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Liolikas.LiolikasGame;
using Liolikas.Scores;

namespace Liolikas.GameMenu
{
    class Menu
    {
        static Menu menu;
        List<MenuSection> menuSections;
        Boolean isShown;
        MenuIterator iter;

         Menu()
         {
             menuSections = new List<MenuSection>();
             AddSection(new MenuSection("New Game", Game.GetGame().StartGame));
             AddSection(new MenuSection("High Scores", HighScore.GetHightScore().ShowHighScore));
             AddSection(new MenuSection("Exit", () => { }));
             iter = new MenuIterator(menuSections);
         }
         public static Menu GetMenu()
         {
             if (menu == null) menu = new Menu();
             return menu;
         }

         internal void AddSection(MenuSection menuSection)
         {
             menuSections.Add(menuSection);
         }

         internal IEnumerable<MenuSection> GetSections()
         {
             return menuSections;
         }

         internal void ShowMenu()
         {
             isShown = true;
             Renderer.RenderMenu(iter.GetCur());
             while (isShown) MenuInput();  
         }

         private void MenuInput()
         {
             ConsoleKey inputKey = Console.ReadKey(true).Key;
             if (inputKey == ConsoleKey.UpArrow) 
             {
                 iter.Prev();
                 Renderer.RenderMenu(iter.GetCur());
             }
             else if (inputKey == ConsoleKey.DownArrow)
             {
                 iter.Next();
                 Renderer.RenderMenu(iter.GetCur());
             }
             else if (inputKey == ConsoleKey.Enter)
             {
                 Menu.GetMenu().isShown = false;
                 iter.GetCur().RunCommand();
             }
         }
    }
}
